//
//  ZMSearchViewController.swift
//  LoveBeen
//
//  Created by pg on 2017/8/8.
//  Copyright © 2017年 pg. All rights reserved.
//

import UIKit

class ZMSearchViewController: UIViewController {

    
    
    //数组属性存储  网络获取最新搜索关键词
    var searchKeyWordsFromWeb: [String] = [String](){
        didSet{
            self.searchCollectionview.keyWordsArray = searchKeyWordsFromWeb
        }
    }
    
    //数组属性,存储搜索到的商品模型
    var searchProductsArray : [ZMProductsModel]?{
        didSet{
            
            self.showProductCollectionView.productDataArray = searchProductsArray
        }
    }
    
    //数组属性,保存之前添加到购物车的商品模型
    var selectedProductsModelArray: [ZMProductsModel]?{
        didSet{
            
        }
    }
    
    //懒加载collectionview
    fileprivate lazy var searchCollectionview: ZMSearchCollectionView = {
       let Collectionview = ZMSearchCollectionView()
        
       return Collectionview
    }()
    
    //懒加载搜索框视图
    fileprivate lazy var searchView : ZMSearchBar = {
        let searchview = ZMSearchBar(frame: CGRect(x: 10, y: 0, width: 300, height: 25))
        searchview.placeholder = "请输入商品的名称"
        return searchview
    }()
    
    //懒加载搜索到商品结果的视图
    fileprivate lazy var showProductCollectionView: ZMSearchProductCollectionView = {
       let showview = ZMSearchProductCollectionView()
        
        
        return showview
    }()
    
    //懒加载购物车图片view
    fileprivate lazy var shopCarView : ZMCuteImageView = {
        let imageview = ZMCuteImageView()
        let image = UIImage(named: "v2_shop_empty")
        imageview.image = image
        
        return imageview
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
 
        self.view.backgroundColor = UIColor.white
        //初始化导航控制器
        setupNav()
        
        //加载网络数据
        loadData()
        
        //初始化主页面
        setupSearchUI()
        
        //注册通知
//        NotificationCenter.default.addObserver(self, selector: #selector(addToShoppingCarViewButtonClick(info:)), name: NSNotification.Name(AddToShoppingCarViewNotification), object: nil)
//        
//        NotificationCenter.default.addObserver(self, selector: #selector(subFromShoppingCarViewButtonClick(info:)), name: NSNotification.Name(SubFromShoppingCarViewNotification), object: nil)
    }
    
    //MARK:- 加载网络数据
    func loadData(){
        //请求搜索关键词
        ZMNetworkingManager.sharedManager.requestSearchKeyWordsData { (keyWordsArray, isSuccess) in
            if isSuccess{
                self.searchKeyWordsFromWeb = keyWordsArray!
            }
        }
    }
    
    //MARK:- 初始化导航控制器
    func setupNav() -> Void {
        
       

        self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: searchView)
        
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "取消",titleColor: UIColor.orange ,target: self, action: #selector(cancleBtnClick))
    }
    
    //MARK:- 导航条点击取消按钮
    func cancleBtnClick() -> Void {
        if self.showProductCollectionView.superview != nil {
            self.showProductCollectionView.removeFromSuperview()
        }
        self.navigationController?.popViewController(animated: true)
    }
    //MARK:- 初始化主页面
    func setupSearchUI(){
        //添加视图
        
        self.view.addSubview(searchCollectionview)
        searchCollectionview.snp.makeConstraints { (make) in
            make.edges.equalTo(view)
        }
        
        searchCollectionview.keyWordsArray = searchKeyWordsFromWeb
        
        searchCollectionview.collectionviweCellSelected = {[unowned self] (selectedText) in

            self.searchView.text = selectedText
            
            //向当前view中添加商品展示窗口
            self.view.addSubview(self.showProductCollectionView)
            self.showProductCollectionView.snp.makeConstraints({ (make) in
                make.edges.equalTo(self.view)
            })
            
            self.view.addSubview(self.shopCarView)
            self.shopCarView.setUp()
            self.shopCarView.title = "12"
            self.shopCarView.snp.makeConstraints { (make) in
                make.trailing.bottom.equalTo(self.view).offset(-5)
                make.width.equalTo(60)
                make.height.equalTo(60)
            }

            
            //发送网络请求
            
            ZMNetworkingManager.sharedManager.requestSearchData(reloadDataCompaltion: { (response, isSuccess) in
               
                if isSuccess{
                    self.showProductCollectionView.productDataArray = response!
                }
            })
        }
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    
}
